/**
 * 新增销售数据
 * Created by xc on 2015/6/12.
 */
define(function (require, exports, module) {
  'use strict';
  module.exports = function (app) {
    app.register.controller('SaleAddCtrl', SaleAddCtrl);
    SaleAddCtrl.$inject = ['$scope', '$filter', 'MHService'];
    function SaleAddCtrl($scope, $filter, MHService) {
      /**
       * 监听输入框
       */
      $scope.$watch('search', function () {
        //name变化时
        if ($scope.search && $scope.search.length > 0) {
          MHService.postReq({url: MHService.URL.search_by_name + '?key=' + $scope.search}, function (data) {
            $scope.feeds = data['datas'];
          });
        }
      }, true);
      /**
       * 选中的楼盘
       * @param feed
       */
      $scope.feedSlected = function (feed) {
        console.log(feed);
        $scope.feeds = undefined;
        $scope.search = undefined;
        $scope.building = {estateName: feed[1], estateId: feed[0]};
      };
      /**
       * 提交销售数据
       * @param data
       */
      $scope.salesAdd = function (_data) {
        var data = angular.copy(_data);
        data.finishTime = $filter('date')(_data.finishTime, 'yyyy-MM-dd');
        MHService.postReq({url: MHService.URL.sales_add, _req: data}, function (result) {
          if (result['success']) {
            MHService.Toast('提交成功');
          } else {
            MHService.Toast('操作失败，请稍后重试');
          }
        });
      }
    }
  }
});
